package com.china08.yxyapi.api.manage.edu.platform.live.lesson;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.live.YxyLiveLesson;
import com.china08.yxyapi.entity.db.live.YxyLiveLessonItem;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.live.YxyLiveLessonItemRepository;
import com.china08.yxyapi.repository.db.live.YxyLiveLessonRepository;
import com.china08.yxyapi.util.DateToolUtils;

@CrossOrigin
@RestController
public class LiveLessonUpdController extends YxyApiBaseController {

    @Autowired
    private YxyLiveLessonRepository yxyLiveLessonRepository;

    @Autowired
    private YxyLiveLessonItemRepository yxyLiveLessonItemRepository;

    @RequestMapping(value = "/api_yw/manage/edu/platform/live/lesson/upd", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        YxyLiveLesson yxyLiveLesson = yxyLiveLessonRepository.findById(reqModel.id);

        RespModel model = new RespModel();
        model.catalogId1 = yxyLiveLesson.getCatalogId1();
        model.catalogId2 = yxyLiveLesson.getCatalogId2();
        model.catalogId3 = yxyLiveLesson.getCatalogId3();
        model.cover = yxyLiveLesson.getCover();
        model.name = yxyLiveLesson.getName();
        model.phone = yxyLiveLesson.getTeacherMobile();
        model.schoolId = yxyLiveLesson.getTeacherSchoolId();
        model.summary = yxyLiveLesson.getSummary();
        model.type = yxyLiveLesson.getLiveType();
        model.userName = yxyLiveLesson.getTeacherNick();
        model.schoolName = yxyLiveLesson.getTeacherSchoolNick();
        List<YxyLiveLessonItem> lessonId = yxyLiveLessonItemRepository.findByLessonIdOrderByBeginDateAsc(reqModel.id);

        List<Pojo> pojos = new ArrayList<LiveLessonUpdController.Pojo>();
        for (YxyLiveLessonItem yxyLiveLessonItem : lessonId) {
            Pojo pojo = new Pojo();
            pojo.bTime = DateToolUtils.format(yxyLiveLessonItem.getBeginDate(), DateToolUtils.FORMAT_LONG);
            pojo.eTime = DateToolUtils.format(yxyLiveLessonItem.getEndDate(), DateToolUtils.FORMAT_LONG);
            pojos.add(pojo);
        }
        model.items = pojos;
        return model;
    }

    public static class ReqModel extends YxyApiReqModel {
        public String id;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(id), "课程ID为空");
        }
    }

    public static class Pojo {
        public String bTime;
        public String eTime;
    }

    public static class RespModel extends YxyApiRespModel {

        public String name;

        public String summary;

        public String cover;

        public String schoolId;

        public String schoolName;

        public String phone;

        public String userName;

        public String catalogId1;

        public String catalogId2;

        public String catalogId3;

        public Integer type;

        public List<Pojo> items;

    }

}
